# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pyside2
pkgbase=mingw-w64-${_realname}-qt5
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-qt5)
pkgdesc="Provides LGPL Qt5 bindings for Python and related tools for binding generation (mingw-w64)"
pkgver=5.14.2
pkgrel=1
arch=('any')
url="https://doc-snapshots.qt.io/qtforpython/"
license=("LGPL")
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-shiboken2-qt5=${pkgver}"
         "${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('staticlibs' 'strip')
install=${_realname}-${CARCH}.install
source=(https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-${pkgver}-src/pyside-setup-opensource-src-${pkgver}.tar.xz
        001-pyside.patch)
sha256sums=('7c7af33792de57255dbdc165c808e1f03a2520295c5922d9897aca8ad1be92b3'
            '9bb699a4c11e038fd156744ee7e772e437dafbd266509d0fa238b4794a42d94d')

prepare() {
  cd "${srcdir}"/pyside-setup-opensource-src-${pkgver}
  patch -p1 -i ${srcdir}/001-pyside.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    ${_conf} \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    ../pyside-setup-opensource-src-${pkgver}/sources/pyside2

  MSYS2_ARG_CONV_EXCL="--include-paths=;--typesystem-paths=" \
  make

}

fix_paths() {
  local prefixwin=$(cygpath -am ${MINGW_PREFIX})
  local prefixinst=$(cygpath -am ${pkgdir}${MINGW_PREFIX})

  local _files=$(find ${pkgdir}${MINGW_PREFIX}/lib -type f -iname "*.cmake" -o -iname "*.pc")
  for f in ${_files}; do
    plain "Patching ${f}..."
    sed -e "s|${prefixwin}|${MINGW_PREFIX}|g" \
        -e "s|${prefixinst}|${MINGW_PREFIX}|g" \
        -i ${f}
  done
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  fix_paths
}
